package com.yhc.my.an_meishia.activities;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.yhc.my.an_meishia.R;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class SaveActivity extends Activity {


    private SimpleAdapter adapter;
    private ListView lvSave;
    private List<Map<String, String>> list;
    private SharedPreferences video;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_save);
        lvSave = (ListView) findViewById(R.id.lv_save);
        TextView tvSave = (TextView) findViewById(R.id.tv_save);
        video = getSharedPreferences("video", MODE_PRIVATE);
        //得到video所有数据
        Map<String, Set<String>> all = (Map<String, Set<String>>) video.getAll();
        Collection<Set<String>> values = all.values();
        Iterator<Set<String>> iterator = values.iterator();
        list = new ArrayList<>();
        while (iterator.hasNext()) {
            Set<String> set = iterator.next();
            Iterator<String> iterator1 = set.iterator();
            Map<String, String> map = new HashMap<>();
            while (iterator1.hasNext()) {
                String next = iterator1.next();
                if (isContainsChinese(next)) {
                    map.put("caption", next);
                } else {
                    map.put("url", next);
                }
            }
            list.add(map);

            adapter = new SimpleAdapter(SaveActivity.this, list, R.layout.save_item, new String[]{"caption"}, new int[]{R.id.tv_save});
            lvSave.setAdapter(adapter);
            lvSave.setEmptyView(tvSave);
        }
        lvSave.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Map<String,String> map =  (Map<String,String>)  lvSave.getItemAtPosition(position);
                Intent intent = new Intent(SaveActivity.this,VideoShowActivity.class);
                intent.putExtra("caption",map.get("caption"));
                intent.putExtra("url",map.get("url"));
                startActivity(intent);
            }
        });


        //长按删除的舰艇
        lvSave.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                SharedPreferences.Editor edit = video.edit();
                Map<String, String> map = (Map<String, String>) lvSave.getItemAtPosition(position);
                edit.remove(map.get("caption"));
                if (edit.commit()) {
                    Toast.makeText(SaveActivity.this, "删除成功", Toast.LENGTH_SHORT).show();
                }
                //删除listview里面的数据
                list.remove(position);
                //改变的舰艇
                adapter.notifyDataSetChanged();
                return true;
            }
        });

    }




    /**
     * 判断字符串是否包含中文
     *
     * @param str
     * @return
     */
    public static boolean isContainsChinese(String str) {
        String regEx = "[\u4e00-\u9fa5]";
        Pattern pat = Pattern.compile(regEx);
        Matcher matcher = pat.matcher(str);
        boolean flg = false;
        if (matcher.find()) {
            flg = true;
        }
        return flg;
    }


}
